<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Content;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use think\Request;

class Uarticle extends Common
{
    public function __construct()
    {

        parent::__construct();
        $ulist = \app\common\model\User::where(1)->select()->toArray();
        View::assign("ulist",$ulist);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        if($user["username"] == 'admin'){
            $where = 1;
        }else{
            $where["uid"] = $user["id"];
        }
        $limit = input("get.limit",10);
        $list =\app\common\model\Content::with(['user'])
            ->where($where)
            ->order("id DESC")
            ->paginate($limit);


        View::assign("list",$list);
        View::assign("title","文章列表");
        return  view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        View::assign("title","文章发布");
        return \view();
    }



    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
       
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        $mod = new \app\common\model\Article();
        $d = $request->post();
        $d["status"] = 1;
        $d["uid"] = $user["id"];
        $d["author"] = $user["nickname"];
        $d["userinfo"] = json_encode($user);
        $res = $mod->save($d);
        if($res){
            return  json(["code"=>200,"msg"=>"添加成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

    public function saveInfo()
    {
        return view();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

        $article = Db::name("article")->find($id);

        View::assign("article",$article);

        if($user = Session::get("index_user")){
            $uid = $user["id"];
            $order = Db::name('article_order')->where(['aid'=>$article['id'],'uid'=>$uid,'status'=>2])->find();
            if($order){

                View::assign('order',$order);
            }else{
                View::assign('order',0);
            }
        }else{

            View::assign('order',0);
        }

        // 上一篇文章和下一篇文章
        $pre_article = Db::name("article")->where("id","<",$id)->order("id DESC")->find();
        $next_article = Db::name("article")->where("id",">",$id)->order("id ASC")->find();

        View::assign('pre_article',$pre_article);
        View::assign('next_article',$next_article);

        return view('read');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $article = Content::with(["article"])
            ->find($id);
        View::assign("article",$article);
        if($article["is_md"] == 1){
            $template = "article/create";
        }else{
            $template = "article/create_edit";
        }

        return \view($template);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //1删除content表 ， 2 删除 article表  - 软删除
        $res = Content::find($id);
        $contentid = $res["id"];
        $res->delete();
        $res2 = \app\common\model\Article::where("contentid",$contentid)
            ->find();
        if($res && $res2){
            return json(["code"=>200,"msg"=>"成功"]);
        }else{
            return json(["code"=>400,"msg"=>"失败"]);
        }
    }

    public function cache_clear()
    {
        Cache::clear();
    }
}
